package com.gitee.hermer.engine.jars.app.sdk.configuration;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.rpc.Filter;
import com.gitee.hermer.boot.jee.commons.log.UtilsContext;
import com.gitee.hermer.engine.jars.core.autoconfigure.annotate.ConditionalOnBean;
import com.gitee.hermer.engine.jars.core.autoconfigure.annotate.ConditionalOnEngineApplication;
import com.gitee.hermer.engine.jars.core.autoconfigure.annotate.ConditionalOnEngineBean;
import com.gitee.hermer.engine.jars.core.domain.EngineConstant;
import com.gitee.hermer.engine.jars.dependent.zipkin.dubbo.rpc.TracingFilter;

import brave.Tracing;

@Configuration
//@Conditional({EngineDubboSpanCondition.class,EngineZipkinSpanCondition.class})
@ConditionalOnEngineApplication
@ConditionalOnBean(type=EngineConstant.CLASS_NAME_ENGINE_APP_DUBBO_CONFIG)
@ConditionalOnEngineBean(name=EngineConstant.SPRING_BEAN_NAME_ZIPKIN_CONFIG)
public class EngineAppDubboZipkinAutoConfiguration extends UtilsContext implements Ordered,ApplicationContextAware{

	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Filter filter = ExtensionLoader.getExtensionLoader(Filter.class).getExtension(EngineConstant.SPRING_BEAN_NAME_ZIPKIN_TRACING_BEAN);  
		if(filter != null){
			((TracingFilter)filter).setTracing(applicationContext.getBean(Tracing.class));
		}
	}



}
